package cn.itcast.musicplayer;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText path;
    private Intent intent;
    private myConn conn;
    MusicService.MyBinder binder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        path =(EditText) findViewById(R.id.et_inputpath);
        findViewById(R.id.tv_play).setOnClickListener(this);
        findViewById(R.id.tv_pause).setOnClickListener(this);
        conn=new myConn();
        intent = new Intent(this,MusicService.class);
        bindService(intent,conn,BIND_AUTO_CREATE);
    }
    private class myConn implements ServiceConnection{
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MusicService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    public void onClick(View v){//播放和暂停的单击事件
        String pathway = path.getText().toString().trim();
        switch (v.getId()){
            case R.id.tv_play:
                if (!TextUtils.isEmpty(pathway)){
                    binder.play(pathway);
                }else {
                    Toast.makeText(this,"找不到音乐文件",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.tv_pause:
                binder.pause();
                break;
                default:
                    break;
        }
    }
    protected void onDestroy(){
        unbindService(conn);
        super.onDestroy();
    }
}
